package com.yuexunit.operationman.nbsy.module.InterventionalOperation.ui;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.InterventionalOperation.Bean.DrugBean;
import com.yuexunit.operationman.nbsy.module.InterventionalOperation.Bean.FlaxmeterBean;
import com.yuexunit.operationman.nbsy.module.InterventionalOperation.Bean.InterNursingBean;
import com.yuexunit.operationman.nbsy.module.InterventionalOperation.adapter.FlaxMeterAdapter;
import com.yuexunit.operationman.nbsy.widget.FixedSwipeListView;

import org.kymjs.kjframe.KJActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/7/24 0024.
 */

public class Act_InterventionalOperation_Preview extends KJActivity {

    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;
    @Bind(R.id.titlebar_docadvice_tv_info)
    LinearLayout titlebarDocadviceTvInfo;
    @Bind(R.id.tv_submit)
    TextView tvSubmit;
    @Bind(R.id.titlebar_advice)
    RelativeLayout titlebarAdvice;
    @Bind(R.id.anesthesia_type)
    TextView anesthesiaType;
    @Bind(R.id.narcotic)
    TextView narcotic;
//    @Bind(R.id.dosage)
//    TextView dosage;
    @Bind(R.id.anesthesia)
    TextView anesthesia;
    @Bind(R.id.anesthesia_start)
    TextView anesthesiaStart;
    @Bind(R.id.anesthesia_end)
    TextView anesthesiaEnd;
    @Bind(R.id.opration_start)
    TextView oprationStart;
    @Bind(R.id.opration_end)
    TextView oprationEnd;
    @Bind(R.id.transfuse_drug)
    TextView transfuseDrug;
    @Bind(R.id.doctor_signature)
    TextView doctorSignature;
    @Bind(R.id.flax_meter)
    TextView flaxMeter;
    @Bind(R.id.listView)
    FixedSwipeListView listView;
    @Bind(R.id.tour)
    TextView tour;

    List<FlaxmeterBean> beanList = new ArrayList<>();
    FlaxmeterBean bean;
    FlaxMeterAdapter flaxMeterAdapter;

    ArrayList<DrugBean> drugBeans =new ArrayList<>();
    ArrayList<DrugBean> OtherDrugBeans =new ArrayList<>();

    @Override
    public void setRootView() {
        setContentView(R.layout.act_interventionaloperation_preview);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        setTitle();
        setContent();
    }

    private void setTitle() {
        tvPatientName.setText(AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getPatientName());
        if (AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getPatientSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getPatientSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getInHospitalNo());
        tvPatientDisease.setText(AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getOperationName());
    }

    private void setContent() {
        if(AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getAnaesthesiaType() != null) {
            if (AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getAnaesthesiaType().equals("1")) {
                anesthesiaType.setText("全麻");
            } else if (AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getAnaesthesiaType().equals("2")) {
                anesthesiaType.setText("局麻");
            }
            else if (AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getAnaesthesiaType().equals("3")) {
                anesthesiaType.setText("表面麻醉");
            }
        }
        if(AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getAnaesthesiaMeasure() != null
                &&AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getAnaesthesiaMeasure().length()>0) {
            String[] medicine = AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getAnaesthesiaMeasure().split(";", -1);
            String content ="";
            for(int i = 0;i<medicine.length;i++)
            {
                content+=medicine[i]+"\n";
            }

            narcotic.setText(content);
        }
        anesthesia.setText(AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getAnestheSign());
        anesthesiaStart.setText(AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getAnesthesiaStartTimeStr());
        anesthesiaEnd.setText(AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getAnesthesiaEndTimeStr());
        oprationStart.setText(AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getOperationStartTimeStr());
        oprationEnd.setText(AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getOperationEndTimeStr());
        doctorSignature.setText(AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getOperationDoctorSign());
        tour.setText(AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getCircuit());

        if( AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getOperationMedicine() != null
                && AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getOperationMedicine().length()>0
                && AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getInjectionTime() != null
                && AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getInjectionTime().length()>0) {
            String[] medicine = AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getOperationMedicine().split(";", -1);
            String[] time = AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getInjectionTime().split(";", -1);

            for (int i = 0; i < medicine.length; i++) {
                DrugBean drugBean = new DrugBean();
                drugBean.setName(medicine[i]);
                drugBean.setStartTime(time[i].split("~", -1)[0]);
                drugBean.setEndTime(time[i].split("~", -1)[1]);
                drugBeans.add(drugBean);
            }

            String content ="";
            for(int i = 0;i<drugBeans.size();i++)
            {
                content+=drugBeans.get(i).getName()+"\n"
                        +"注射开始时间"+"\n"+drugBeans.get(i).getStartTime()+"\n"
                        +"注射结束时间"+"\n"+drugBeans.get(i).getEndTime()+"\n"
                        +"\n";
            }

            transfuseDrug.setText(content);
        }

        if(AppConfig.INTERNURSEBEAN.getObject().getInterNursingItemList() != null)
        {
            String[] P = new String[0],BP = new String[0],OS = new String[0],Time = new String[0];
            for (InterNursingBean.ObjectEntity.InterNursingItemListEntity p : AppConfig.INTERNURSEBEAN.getObject().getInterNursingItemList()) {
                switch (p.getItemCode()) {
                    case "P":
                        if (p.getItemValue().length() > 0) {
                            if(p.getItemValue().substring(p.getItemValue().length()-1,p.getItemValue().length()).equals(";"))
                            {
                                P = p.getItemValue().substring(0,p.getItemValue().length()-1).split(";", -1);
                            }
                            else {
                                P =  p.getItemValue().split(";", -1);
                            }
                        }
                        break;
                    case "BP":
                        if (p.getItemValue().length() > 0) {
                            if (p.getItemValue().substring(p.getItemValue().length() - 1, p.getItemValue().length()).equals(";")) {
                                BP = p.getItemValue().substring(0, p.getItemValue().length() - 1).split(";", -1);
                            } else {
                                BP = p.getItemValue().split(";", -1);
                            }
                        }
                        break;
                    case "OS":
                        if (p.getItemValue().length() > 0) {
                            if (p.getItemValue().substring(p.getItemValue().length() - 1, p.getItemValue().length()).equals(";")) {
                                OS = p.getItemValue().substring(0, p.getItemValue().length() - 1).split(";", -1);
                            } else {
                                OS = p.getItemValue().split(";", -1);
                            }
                        }
                        break;
                    case "Time":
                        if (p.getItemValue().length() > 0) {
                            if (p.getItemValue().substring(p.getItemValue().length() - 1, p.getItemValue().length()).equals(";")) {
                                Time = p.getItemValue().substring(0, p.getItemValue().length() - 1).split(";", -1);
                            } else {
                                Time = p.getItemValue().split(";", -1);
                            }
                        }
                        break;
                }
            }

            for (int i = 0;i<Time.length;i++)
            {
                bean = new FlaxmeterBean();
                bean.setTime(Time[i]);
                if( BP.length>i&&BP[i].length() > 0) {
                    String[] skin = BP[i].split("/", -1);
                    bean.setDbp(skin[0]);
                    bean.setSbp(skin[1]);
                }
                else {
                    bean.setDbp("");
                    bean.setSbp("");
                }
                if(P.length>i) {
                    bean.setRate(P[i]);
                }
                else {
                    bean.setRate("");
                }
                if(OS.length>i) {
                    bean.setSaturation(OS[i]);
                }
                else {
                    bean.setSaturation("");
                }
                beanList.add(bean);
            }

            flaxMeterAdapter = new FlaxMeterAdapter(listView, beanList, R.layout.item_flaxmeter_view);
            listView.setAdapter(flaxMeterAdapter);
        }
    }

    @OnClick({R.id.titlebar_docadvice_img_back, R.id.tv_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.tv_submit:
                finish();
                break;
        }
    }
}
